<?php

namespace App\Http\Logic\PC;

use App;
use App\Helper\Helper;
use App\Models\Users as usersModel;
use App\Common\Status;
use Illuminate\Support\Facades\DB;


class AuthLogic
{
    use App\Http\Logic\NoteOtherLogic;

    public static function loginCheck($mobile)
    {
        $where['mobile'] = $mobile;
        $user_mes        = usersModel::where($where)->first();
        if ($user_mes) {
            return $user_mes;
        } else {
            return [];
        }
    }

    /**
     * @param $mobile
     */
    public static function createUser($mobile)
    {
        $strs       = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
        $name       = substr(str_shuffle($strs), mt_rand(0, strlen($strs) - 11), 6);
        $connection = DB::connection('mysql');
        $connection->beginTransaction();
        try {
            $data['mobile']          = $mobile;
            $data['area_code']       = "+86";
            $data['nickname']        = $name;
            $data['register_source'] = 7;   # 招聘网
            $data['create_time']     = time();
            $uid                     = DB::table('gz_users')->insertGetId($data);
            $uniqid                  = uniqid() . time();
            if ($uid) {
                $infoData['user_id']     = $uid;
                $infoData['user_uniqid'] = $uniqid;
                $infoData['create_time'] = time();
                DB::table('gz_users_info')->insert($infoData);

                $authData['user_id']       = $uid;
                $authData['identity_type'] = 7;
                $authData['create_time']   = time();
                DB::table('gz_user_auth')->insert($authData);
            }

            $connection->commit();
            return ['uid' => $uid, "uniqid" => $uniqid, "mobile" => $mobile, "name" => $name];
        } catch (\Exception $exception) {
            DB::rollBack();

            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * @param $uid
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null
     */
    public static function getUuid($uid)
    {
        $where['user_id'] = $uid;
        return DB::table('gz_users_info')->where($where)->first();
    }


    /**
     * @param $uid
     * @return array
     */
    public static function getUserInfo($uid)
    {
        $mobile = DB::table('gz_users')->find($uid);

        if (!$mobile) {
            return ["status" => 4001, "msg" => "用户信息有误"];
        }
        if (!$mobile->mobile) {
            return ["status" => 4001, "msg" => "用户未绑定手机号"];
        }
        $res["mobile"] = $mobile->mobile;

        # 附件简历
        $jl = DB::table('recruit_notes_middle')->where(
            [
                ["mobile", "=", $mobile->mobile],
                ["is_deleted", "=", Status::NOT_DELETE],
                ["status", "=", Status::REGULAR]
            ]
        )->first();


        # 头像
        $head = DB::table('recruit_notes as a')
            ->leftJoin('recruit_resources as b', function ($join) {
                $join->on("a.head_img", "=", "b.id")
                    ->where("b.status", '=', Status::REGULAR)
                    ->where("b.is_deleted", '=', Status::NOT_DELETE);
            })
            ->where(
                [
                    ["a.mobile", "=", $mobile->mobile],
                    ["a.is_deleted", "=", Status::NOT_DELETE],
                    ["a.status", "=", Status::REGULAR]
                ]
            )
            ->select(["b.path", "a.id"])
            ->first();

        $res["head_img"] = $res["note_id"] = "";
        if ($head) {
            $res["head_img"] = $head->path ? Helper::buildQiNiuUrl($head->path) : "";
            $res["note_id"]  = $head->id;
        }
        $res['annex_path'] = $res['att_name'] = $res['annex_created_at'] = "";
        $tmp            = self::GetAnnex($mobile->mobile);
        if ($tmp && count($tmp) > 0) {
            $res['annex_path']  = $tmp["path"];
            $res['att_name']  = $tmp["name"];
            $res['annex_created_at']  = $tmp["created_at"];
        }

        $res["user_id"] = $uid;
        $res["name"]    = $mobile->nickname;
        $res["res_id"]  = $jl ? $jl->res_id : null;
        $res["status"]  = Status::SUCCESS;

        return $res;
    }

    /**
     * @param $params
     * @return array|int
     */
    public static function bindMobile($params)
    {
        # 是否相同手机号
        $info = DB::table('gz_users')->where("mobile", $params["mobile"])->first();

        if ($info) {
            return ["status" => Status::ERROR, "msg" => "该手机号已被注册！"];
        }
        $bool = DB::table('gz_users')->where("id", $params["id"])->update(["mobile" => $params["mobile"]]);
        if (!$bool) {
            return ["status" => Status::ERROR, "msg" => "绑定失败请重试！"];
        }
        return $bool;
    }


    /**
     * @param $params
     * @return array|bool
     */
    public static function checkMobile($params)
    {
        $info = DB::table('gz_users')->where("mobile", $params["mobile"])->first();

        if (!$info) {
            return ["status" => Status::ERROR, "msg" => "未注册！"];
        }

        return true;
    }
}
